<?php

namespace app\api\controller;

use app\admin\model\Counselor;
use app\admin\model\GoodsInquiry;
use app\admin\model\Service;
use app\admin\model\Store;
use app\common\controller\Api;
use app\common\model\Category;
use think\Validate;

/**
 * 原石接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 原石列表
     *
     */
    public function page()
    {
        $category_id = input('category_id',0);

        $province = input('province_id',0);

        $city = input('city_id',0);

        $type = input('type','all');

        $is_recommend = input('is_recommend',0);

        $order = input('order','');

        $price_type = input('price_type',0);

        $keyword = input('keyword','');

        $goods_type = input('goods_type','all');

        $where = [];

        if ($category_id){
            $where['category_id'] = $category_id;
        }

        if ($is_recommend){
            $where['is_recommend'] = 1;
        }

        if ($keyword){
            $where['name'] = ['like', '%' . $keyword . '%'];
        }
        if ($type!='all'){
            $where['type'] = $type;
        }
        if ($goods_type!='all'){
            $where['goods_type'] = $goods_type;
        }
        if ($province){
            $where['province'] = $province;
        }
        if ($city){
            $where['city'] = $city;
        }
        switch ($price_type){
            case '1':
                $where['price'] = ['<',10];
                break;
            case '2':
                $where['price'] = ['between',[10,20]];
                break;
            case '3':
                $where['price'] = ['between',[20,30]];
                break;
            case '4':
                $where['price'] = ['between',[30,50]];
                break;
            case '5':
                $where['price'] = ['between',[50,70]];
                break;
            case '6':
                $where['price'] = ['between',[70,100]];
                break;
            case '7':
                $where['price'] = ['>',200];
                break;
        }

        if ($order=='new'){
            $orderBy = 'createtime desc';
        } else {
            $orderBy = 'weigh desc';
        }


        $lists = \app\admin\model\Goods::where($where)
            ->field('id,category_id,name,image,price')
            ->order($orderBy)
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('请求成功',$lists);
    }

    //原石详情
    public function info()
    {
        $goods_id = input('goods_id',0);

        $info = \app\admin\model\Goods::get($goods_id);

        if ($info['store_id']){
            $store_info = Store::where(['id'=>$info['store_id']])->field('id,name,images,mobile,start_time,end_time,address,lng');
            $info['store_info'] = $store_info;
        }

        if ($info['counselor_id']){
            $counselor_info = Counselor::get($info['counselor_id']);
            $info['counselor_info'] = $counselor_info;
        }

        if ($info['service_ids']){
            $service_list = Service::where('id','in',$info['service_ids'])->select();
            $info['service_list'] = $service_list;
        }

        if ($info['process']){

            $info['process'] = json_decode($info['process'],true);
        }

        $this->success('请求成功',$info);
    }

    //原石动态
    public function dynamic()
    {
        $goods_id = input('goods_id',0);

        $list = \app\admin\model\GoodsDynamic::where(['goods_id'=>$goods_id])->select();
        $this->success('请求成功',$list);
    }

    //原石询价提交
    public function inquiry()
    {
        $feedback_model = new GoodsInquiry();

        $data = [
            'user_id'=>$this->auth->id,
            'goods_id'=>input('goods_id',''),
            'mobile'=>input('mobile',''),
        ];

        if (empty($data['goods_id'])){
            $this->error('缺少参数原石id');
        }
        if (empty($data['mobile'])){
            $this->error('请输入联系方式');
        }
        if (!Validate::regex($data['mobile'], "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $res = $feedback_model->save($data);

        $this->success('请求成功',$res);

    }

    //门店详情
    public function storeInfo()
    {
        $store_id = input('store_id',0);
        $longitude = input('longitude','');
        $latitude = input('latitude','');
        $store_info = Store::get($store_id);
        if (!empty($longitude) && !empty($latitude)){
            if ($store_info[ 'lng' ] && $store_info[ 'lat' ]) {
                $distance = getDistance((float) $store_info[ 'lng' ], (float) $store_info[ 'lat' ], (float) $longitude, (float) $latitude);
                $store_info[ 'distance' ] = round($distance / 1000,1);
            } else {
                $store_info[ 'distance' ] = 0;
            }
        } else {
            $store_info[ 'distance' ] = 0;
        }

        $this->success('请求成功',$store_info);
    }

    //原石品种
    public function category()
    {
        $category = Category::where(['status'=>'normal'])
            ->order('weigh desc')->field('id,name')->select();
        $this->success('请求成功',$category);
    }

    //价格筛选
    public function price()
    {
        $data = [
            0=>'不限价格',
            1=>'10万以下',
            2=>'10-20万',
            3=>'20-30万',
            4=>'30-50万',
            5=>'50-70万',
            6=>'70-100万',
            7=>'200万以上',
        ];
        $this->success('请求成功',$data);
    }

}
